const path = require('path');
const libversion = require('./package.json').version;
const TerserPlugin = require('terser-webpack-plugin');

const packmin = false;
const usees5 = false;

module.exports = (env, argv) => {
    const isProduction =  false;
    const isPackMin = packmin;
    const isUseES5 = usees5;

    return {
        mode: isProduction ? 'production' : 'development',
        entry: './src/layui-ko.debug.js',
        output: {
            filename: `layui-ko-${libversion}${isPackMin ? ".min" : ""}.js`,
            path: path.resolve(__dirname, 'dist'),
            libraryTarget: 'umd',
            umdNamedDefine: true,
            globalObject: 'this',
            chunkFormat: 'array-push',
        },
        optimization: {
            minimize: true,
            minimizer: [
                new TerserPlugin({
                    include: /\.min\.js$/,
                    terserOptions: {
                        ecma: isUseES5 ? 5 : 6,
                    },
                }),
            ],
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: isUseES5 ? {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env'],
                            plugins: [
                                '@babel/plugin-proposal-class-properties'
                            ],
                        },
                    } : undefined,
                },
            ],
        },
        target: isUseES5 ? 'es5' : 'es6',
    };
};